PHP OOP: Object inheritance
November 20, 2020
extend
keyword allows a child class to inherit from another class' methods and properties (the parent class). class ChildClass extends PerentClass {
// Do some stuff in here...
// We can use the parent::abc(); attribute to get the result of the parent abc() method and change it.
}
<?php
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Counter.php'; // 1. Call the parent class file
class CounterDouble extends Counter { // 2. tell this is an extension of the parent class
public function get_views(): int
{
return 2 * parent::get_views(); // 3. Just change the output as needed
}
}
<?php
class Counter {
public $file;
public function __construct(string $var1)
{
$this->file = $var1;
}
public function increment(): void
{
if (file_exists($this->file)) {
$views = (int)file_get_contents($this->file);
$views++;
} else {
$views = 1;
}
file_put_contents($this->file, $views);
}
public function get_views(): int
{
if (!file_exists($this->file)) {
return 0;
}
return (int)file_get_contents($this->file);
}
}
<div class="col-md-4">
<h5>Counter Double</h5>
<ul class="list-unstyled">
<li>
<?php
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'CounterDouble.php';
$counter = new CounterDouble(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'page-views');
$counter->increment();
$pageViews = $counter->get_views();
echo "<b>$pageViews</b> page view";
if ($pageViews > 1) { echo "s"; }
?>
</li>
</ul>
</div>
// CounterDouble.php
<?php
class CounterDouble {
public $file;
public function __construct(string $var1)
{
$this->file = $var1;
}
public function increment(): void
{
if (file_exists($this->file)) {
$views = (int)file_get_contents($this->file);
$views++;
} else {
$views = 1;
}
file_put_contents($this->file, $views);
}
public function get_views(): int
{
if (!file_exists($this->file)) {
return 0;
}
return 2 * (int)file_get_contents($this->file);
// This is the only modification of the code...
}
}
private static function abc()
protected static function abc()
CONSTANT
.self::
to use it inside a class method. example: self::CONSTANT
static::
attribute in the parent class to allow the constant to be shared in childrens ones (have a look to the code below!)
<?php
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Counter.php';
class Counter10x extends Counter {
const INCREMENT = 10;
}
<?php
class Counter {
const INCREMENT = 1;
public $file;
public function __construct(string $var1)
{
$this->file = $var1;
}
public function increment(): void
{
if (file_exists($this->file)) {
$views = (int)file_get_contents($this->file);
$views += static::INCREMENT;
// Note: we use the static:: attribute here instead of self:: as for regular properties
// (using self:: the property would refer to the parent and not child constante)!
} else {
$views = 1;
}
file_put_contents($this->file, $views);
}
public function get_views(): int
{
if (!file_exists($this->file)) {
return 0;
}
return (int)file_get_contents($this->file);
}
}